<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Admin;
use think\Loader;
use think\Db;
use think\Session;
use think\Cookie;
use think\Request;//首先引入 Request 文件；

header('Content-Type:application/json; charset=utf-8');
header("Access-Control-Allow-Origin: *");//允许跨域请求

class Login extends Controller
{

    public function index(){
        if(Request()->isPost()){
            $name = input('name');
            $log_psd=md5(input('log_psd'));
            $role=input('radio'); //用户角色
                if($role==1){
                  $res_name = Db::name('broker')->where("log_id = '$name'")->find();
                  if(!$res_name){
                    $log_info['message']='2';//用户不存在
                    return json_encode($log_info,JSON_UNESCAPED_UNICODE);
                  }
                  $res=Db::name('broker')->where("log_id = '$name' AND log_psd = '$log_psd'")->find();
                  if(!$res){
                    $log_info['message']='3';//密码错误
                    return json_encode($log_info,JSON_UNESCAPED_UNICODE);
                  }else{
                    //判断此用户一分钟内是否登录20次
                    $log_time=Db::table('tp_logon_log')->where("cid='".$res['cid']."'")->order('time asc')->limit(20)->select();
                    if(count($log_time)>=20 && ($log_time[19]['time']-$log_time[0]['time'])<=60){ //一分钟内登录超20次
                       $log_info['message'] = '4';//你一分钟内登录超20次
                       return json_encode($log_info,JSON_UNESCAPED_UNICODE);
                    }else{
                    //需要传递的数据
                    $logon_log=[
                      'cid'  => $res['cid'],
                      'sid'  => request()->ip(),
                      'time' => time(),
                      'name' => $res['name']
                    ];

                    Db::name('logon_log')->insert($logon_log);//存入登录日志表

                    $log_info['message'] = '1';//登录成功
                    $log_info['role']    = $role;
                    $log_info['name']    = $res['name'];
                    $log_info['registry']= $res['registry'];//执业认证状态
                    $log_info['exam']    = $res['exam'];//是否通过考试
                    $log_info['log_id']  = $res['log_id'];
                    $log_info['cid']     = $res['cid'];


                    //$log_info['test']  = $_SESSION['cid'];
                    //$this->success('登录成功！','index/index','',2);
                    return json_encode($log_info,JSON_UNESCAPED_UNICODE);
                   }
                  }
                }elseif($role==2){
                  $res_name = Db::name('natural_gas_company')->where("cname = '$name'")->find();
                  if(!$res_name){
                    $log_info['message']='2';
                    return json_encode($log_info,JSON_UNESCAPED_UNICODE);
                  }
                  $res=Db::name('natural_gas_company')->where("cname = '$name' AND log_psd = '$log_psd'")->find();
                  if(!$res){
                    $log_info['message']='3';
                    return json_encode($log_info,JSON_UNESCAPED_UNICODE);
                  }else{
                    //信息正确，传递信息
                    $log_info['message'] = '1';
                    $log_info['role']    = $role;
                    $log_info['name']    = $name;//公司名称
                    $log_info['comid']   = $res['comid'];//公司编码
                    $log_info['tel']     = $res['tel'];
                    $log_info['tname']   = $res['tname'];
                    $log_info['address'] = $res['address'];
                    $log_info['content'] = $res['content'];

                    session_start();
                    $_SESSION['comid']=$res['comid'];
                    $_SESSION['name']=$name;
                    $_SESSION['role']=$role;

                    //$this->success('登录成功！','index/index','',2);
                    return json_encode($log_info,JSON_UNESCAPED_UNICODE);
                }
            }else{
               $res_name = Db::name('product')->where("bname = '$name'")->find();
               if(!$res_name){
                    $log_info['message']='2';
                    return json_encode($log_info,JSON_UNESCAPED_UNICODE);
                  }
                  $res=Db::name('product')->where("bname = '$name' AND log_psd = '$log_psd'")->find();
                  if(!$res){
                    $log_info['message']='3';
                    return json_encode($log_info,JSON_UNESCAPED_UNICODE);
                  }else{
                    //信息正确，传递信息
                    $log_info['message'] = '1';
                    $log_info['role']    = $role;
                    $log_info['name']    = $name;//公司名称
                    $log_info['tel']     = $res['tel'];
                    $log_info['tname']   = $res['tname'];
                    $log_info['bcontent']= $res['bcontent'];
                    $log_info['status']  = $res['status'];

                    //$_SESSION['status']=$res['status'];
                   // $_SESSION['name']=$name;
                    //$_SESSION['role']=$role;
                    //return $this->success('登录成功！','index/index','',2);
                    return json_encode($log_info,JSON_UNESCAPED_UNICODE);
                  }
            }
        }//request
        return $this->fetch('login');
    }//方法
   public function zhuce(){
        if(request()->isPost()){
            $data=[
               'cid'=>GetRandStr(10),
               'log_id'=>input('log_id'),
               'log_psd'=>md5(input('log_psd')),
               'name'=>input('name'),
               'sex'=>input('sex'),
               'type'=>input('type'),
               'idcard'=>input('idcard'),
               'work'=>input('work'),
               'tel'=>input('tel'),
               'email'=>input('email'),
               'address'=>input('address'),
               'comid'=>input('comid'),
               'wid'=>input('wid'),
            ];

            $validate = Loader::validate('Admin');
            if(!$validate->scene('add')->check($data)){  //应用验证
               $arr=array('message'=>$validate->getError());
               return json_encode($arr,JSON_UNESCAPED_UNICODE);exit;
            }

            if(db('broker')->insert($data)){ //助手函数添加
                //return $this->success('注册经济人成功！','index','',2);
                $register['message']='yes';
                return json_encode($register,JSON_UNESCAPED_UNICODE);
            }else{
                //return $this->error('注册经济人失败！');
                $register['message']='no';
                return json_encode($register,JSON_UNESCAPED_UNICODE);
            }
        }
        //return $this->fetch();
    }


    public function logout(){
    session_start();
    session_destroy();
    $arr=array('logout_message'=>'退出成功');
    return json_encode($arr,JSON_UNESCAPED_UNICODE);
        //$this->success('退出成功','login/index','',2);
    }

}
